﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource;

namespace FlyHorse.LIMS.WinApp.BaseManageForms.CustomerForms
{
    public partial class CustomerMainForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.CustomerBll customerBll = new BLL.CustomerBll();
        public CustomerMainForm()
        {
            InitializeComponent();
        }
        private void CustomerMainForm_Load(object sender, EventArgs e)
        {
            this.gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
            this.gridView1.OptionsBehavior.Editable = false;
            this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
            InitGrid();
        }

        private void InitGrid()
        {
            var models = customerBll.LoadEntities(m => true).OrderByDescending(m=>m.AddDateTime).ThenByDescending(m => m.CustomerID).ToList();
            this.gridControl1.DataSource = models;
        }

        private void ButtonItem_add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CustomerAddForm customerAddForm = new CustomerAddForm(InitGrid);
            customerAddForm.Show();
        }

       

        private void ButtonItem_Delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //\n\n\n 如果是新增客户的时候填写错误可以删除后重新填写，如果系统中已经有此客户的报告不要删除客户 
            if (this.gridView1.GetFocusedRowCellValue("CustomerID") == null)
            {
                return;
            }
            DialogResult result = XtraMessageBox.Show(ResourceOverall.ConfirmDelete, ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (result == DialogResult.OK)
            {
                using (OJCB4Entities db =new OJCB4Entities())
                {
                    string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("CustomerID"));
                    int count = db.ReportProducts.Where(m => m.CustomerID == id).Count();
                    if (count > 0)
                    {
                        XtraMessageBox.Show(ResourceOverall.DeleteExists);
                        return;
                    }
                    Customer customer = db.Customers.Where(m => m.CustomerID == id).FirstOrDefault();
                    db.Customers.Remove(customer);
                    bool res = db.SaveChanges()>0;
                    if (res)
                    {
                        InitGrid();
                        XtraMessageBox.Show(ResourceOverall.DeleteSucceed);
                    }
                }
            }
        }

        private void barLargeButton_Edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.gridView1.GetFocusedRowCellValue("CustomerID") == null)
            {
                return;
            }
            string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("CustomerID"));
            Customer custtom = customerBll.LoadEntities(m => m.CustomerID == id).FirstOrDefault();
            if (custtom != null)
            {
                CustomerAddForm form = new CustomerAddForm(custtom, InitGrid);
                form.ShowDialog();
            }
        }


    }
}